package com.flashcash.kotlin.activity;

import android.support.v7.app.AlertDialog
import android.webkit.*
import com.flashcash.solu.BuildConfig
import com.flashcash.solu.R
import com.flashcash.solu.app.base.BaseActivity
import com.flashcash.solu.app.base.BaseActivityView
import com.flashcash.solu.app.base.presenter.BaseActivityPresenter
import com.flashcash.solu.app.base.presenter.BaseActivityPresenterImpl
import org.jetbrains.anko.find

class SnatchBotActivity : BaseActivity<SnatchBotPresenter>(), SnatchBotView {
    override fun getLayoutResId() =
            R.layout.activity_snatch_bot_web

    private lateinit var webView: WebView

    override fun init() {
        showLoading("Loanding...");
        webView = find(R.id.snatch_bot_web_view)
        webView.apply {
            settings.setAppCachePath(cacheDir.absolutePath)
            settings.setUserAgentString("Android")
            settings.setAppCacheEnabled(true)
            settings.setSupportZoom(true)
            settings.javaScriptEnabled = true
            webChromeClient = object : WebChromeClient() {
                override fun onProgressChanged(view: WebView?, newProgress: Int) {
                    if (newProgress >= 99) {

                        dismissLoading()
                    }
                }

            }
            webViewClient = object : WebViewClient() {
                override fun onPageFinished(view: WebView?, url: String?) {
                    dismissLoading()
                }

                override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
                    AlertDialog.Builder(context)
                            .setTitle(R.string.snatch_bot_error_title)
                            .setMessage("${R.string.snatch_bot_error_message}${BuildConfig.SNATCH_BOT_CONTACT_EMAIL}")
                            .setCancelable(false)
                            .setPositiveButton("Back", { dialog, which -> onBackPressed() })
                            .create()
                            .show()
                    super.onReceivedError(view, request, error)
                }
            }
            loadUrl(BuildConfig.SNATCH_BOT_WEB_URL)


        }

    }

    override fun initPresenterImpl() = SnatchBotPreImpl()

    override fun onBackPressed() {
//        if (webView.canGoBack()) {+
//            webView.goBack()
//        }else {
        super.onBackPressed()
//        }
    }
}

class SnatchBotPreImpl : SnatchBotPresenter, BaseActivityPresenterImpl() {

}

interface SnatchBotPresenter : BaseActivityPresenter {

}

interface SnatchBotView : BaseActivityView {

}